#!/bin/sh
# postinst script for svnyoungest
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

webserver=apache2
webuser=www-data
installpath=/usr/share/playsms
binpath=$installpath/bin
dbcnf_mysqluser='playsms/mysql-username'
dbcnf_mysqlpass='playsms/mysql-password'

if [ "$1" = "configure" ]; then
     # start up debconf and tell it the protocol version
     #
     . /usr/share/debconf/confmodule
     db_version 2.0

     # tell debconf to get the username/password
     # that we asked the user for earlier
     # (in the config script)
     #
     db_get $dbcnf_mysqluser
     mysqlusername="$RET"
     db_get $dbcnf_mysqlpass
     mysqlpassword="$RET"

     # reset the username/password; this way
     # we won't have sensitive data 
     # in the debconf database...
     #
     db_reset $dbcnf_mysqluser || true
     db_reset $dbcnf_mysqlpass || true
     db_fset  $dbcnf_mysqluser seen false || true
     db_fset  $dbcnf_mysqlpass seen false || true

     # run the mysql install scripts using
     # the username and password
     #
     mysql --user=$mysqlusername --password=$mysqlpassword < $installpath/db/playsms-install.sql
     mysql --user=$mysqlusername --password=$mysqlpassword < $installpath/db/playsms-makedb.sql


     # give apache user sudo rights for our scripts
     # by adding the webuser to the sudoers file, but
     # make sure we haven't already done so in some past install
     #
     webuser_sudo="$webuser ALL=NOPASSWD: $binpath/*"
     set +e
     grep -q "$webuser_sudo" /etc/sudoers
     ret=$?
     set -e
     if [ "$ret" != "0" ]; then
        echo "\n\n$webuser_sudo" >> /etc/sudoers
     fi


     # add the kannel user to group 'dialout'
     # so that it can use bluetooth devices
     # that have been bound to /dev/rfcomm*,
     # which are usually root:dialout
     #
     adduser kannel dialout


     # reload services whose confs we've modified
     #
     if [ -x /usr/sbin/invoke-rc.d ]; then
          invoke-rc.d $webserver reload || true
          invoke-rc.d kannel restart || true
     else
          /etc/init.d/$webserver reload || true
          /etc/init.d/kannel restart || true
     fi   


     # quit debconf
     #
     db_stop
fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


